import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../data/message_data.dart';

class ChatMessage extends StatelessWidget {
  final MessageData messageData;

  ChatMessage({required this.messageData});

  @override
  Widget build(BuildContext context) {
    final ThemeData themeData = Theme.of(context);
    final Color textColor = messageData.isMe
        ? Colors.white
        : themeData.accentTextTheme.bodyText1!.color!;
    final Color backgroundColor =
        messageData.isMe ? themeData.accentColor : Colors.grey[300]!;
    final MainAxisAlignment mainAxisAlignment =
        messageData.isMe ? MainAxisAlignment.end : MainAxisAlignment.start;
    final CrossAxisAlignment crossAxisAlignment =
        messageData.isMe ? CrossAxisAlignment.end : CrossAxisAlignment.start;
    // 增加头像

    return Container(
      margin: const EdgeInsets.symmetric(vertical: 10.0),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: mainAxisAlignment,
            crossAxisAlignment: crossAxisAlignment,
            children: <Widget>[
              if (!messageData.isMe) MessageHeader(messageData: messageData),
              MessageBody(messageData: messageData),
              // 添加我的头像
              if (messageData.isMe) MessageHeader(messageData: messageData),
            ],
          ),
          // MessageTime(timeMillis: messageData.timeMillis),
        ],
      ),
    );
  }
}

class MessageTime extends StatelessWidget {
  final String timeMillis;

  MessageTime({required this.timeMillis});

  @override
  Widget build(BuildContext context) {
    // 返回聊天时间 居中 背景色为灰色
    return Container(
      margin: const EdgeInsets.symmetric(vertical: 10.0),
      child: Center(
        child: Container(
          padding: const EdgeInsets.all(5.0),
          decoration: BoxDecoration(
            color: Colors.grey,
            borderRadius: BorderRadius.circular(5.0),
          ),
          child: Text(
            _getTime(timeMillis),
            style: TextStyle(
              color: Colors.white,
              fontSize: 12.0,
            ),
          ),
        ),
      ),
    );
  }

  /// 换算时间为刚刚 几分钟前 一小时前 一天前 一周前 一月前 一年前
  String _getTime(String timeMillis) {
    // 获取当前时间
    DateTime now = DateTime.now();
    // 获取传入时间
    DateTime dateTime =
        DateTime.fromMillisecondsSinceEpoch(int.parse(timeMillis));
    // 获取时间差
    Duration duration = now.difference(dateTime);
    // 获取时间差的秒数
    int seconds = duration.inSeconds;
    // 获取时间差的分钟数
    int minutes = duration.inMinutes;
    // 获取时间差的小时数
    int hours = duration.inHours;
    // 获取时间差的天数
    int days = duration.inDays;
    // 获取时间差的周数
    int weeks = days ~/ 7;
    // 获取时间差的月数
    int months = days ~/ 30;
    // 获取时间差的年数
    int years = days ~/ 365;
    // 判断时间差
    if (seconds < 60) {
      return "刚刚";
    } else if (minutes < 60) {
      return "$minutes 分钟前";
    } else if (hours < 24) {
      return "$hours 小时前";
    } else if (days < 7) {
      return "$days 天前";
    } else if (weeks < 4) {
      return "$weeks 周前";
    } else if (months < 12) {
      return "$months 月前";
    } else {
      return "$years 年前";
    }
  }
}

class MessageHeader extends StatelessWidget {
  final MessageData messageData;

  MessageHeader({required this.messageData});

  @override
  Widget build(BuildContext context) {
    final ThemeData themeData = Theme.of(context);
    final Color backgroundColor =
        messageData.isMe ? themeData.accentColor : Colors.grey;
    final Color iconColor =
        messageData.isMe ? Colors.white : themeData.accentColor;
    return Container(
      margin: const EdgeInsets.only(right: 16.0),
      child: CircleAvatar(
        backgroundColor: backgroundColor,
        child: Icon(
          messageData.avatar,
          color: iconColor,
        ),
      ),
    );
  }
}

class MessageBody extends StatelessWidget {
  final BorderRadius _borderRadius = BorderRadius.circular(12.0);
  final MessageData messageData;

  MessageBody({required this.messageData});

  @override
  Widget build(BuildContext context) {
    final ThemeData themeData = Theme.of(context);
    final Color textColor =
        messageData.isMe ? Colors.white : const Color(0XFF333333);
    final Color backgroundColor =
        messageData.isMe ? themeData.accentColor : Colors.grey;
    return Container(
      //设置最大宽度
      constraints: const BoxConstraints(maxWidth: 200.0),
      margin: const EdgeInsets.only(right: 16.0),
      decoration: BoxDecoration(
        borderRadius: _borderRadius,
        color: backgroundColor,
      ),
      child: Padding(
        padding: const EdgeInsets.all(12.0),
        child: Text(
          messageData.message,
          style: TextStyle(color: textColor),
        ),
      ),
    );
  }
}
